package _mst;

import _object.ListNode;
import _object.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * 面试题 04.03. 特定深度节点链表
 */
public class T0403 {
    public ListNode[] listOfDepth(TreeNode tree) {
        List<ListNode> ans = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(tree);
        while (!queue.isEmpty()) {
            int size = queue.size();
            ListNode listNode = new ListNode(-1), dummy = listNode;
            for (int i = 0; i < size; i++) {
                TreeNode poll = queue.poll();
                listNode.next = new ListNode(poll.val);
                listNode = listNode.next;
                if (poll.left != null) queue.offer(poll.left);
                if (poll.right != null) queue.offer(poll.right);
            }
            ans.add(dummy.next);
        }
        return ans.toArray(new ListNode[0]);
    }
}
